/***************************************************************************
 *   mgpi_grbclr.c - Garbage Collector Definitions                         *
 *   Copyright (C) 2008 by cmaster.matso                                   *
 *   cmaster.matso@gmail.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "mgpi_grbclr.h"

static void *s_aGarbage[MGPI_MAX_COLLECTOR_CAPACITY];                           /** (?) */
static int s_nTn = 0, s_nLn = 0;                                                /** ???? */
static lgarb_t s_oGarbage = {NULL, NULL, NULL};                                 /** garbage handle */

/**
 * Collects given pointer in the array.
 */
void MGPI_CollectGarbageA(void *ptrgrb) {
    s_aGarbage[s_nTn++] = ptrgrb;
    return;
}

/**
 * Frees all not NULL pointers in the array.
 */
int MGPI_RemoveGarbageA(void) {
    int i = 0;
    while (i < s_nTn) if (s_aGarbage[i]) free(s_aGarbage[i++]);
    return s_nTn;
}

/**
 * Collets given pointer in the list.
 */
void MGPI_CollectGarbage(void *ptrgrb) {/* MODIFICATION: check the number of garbage according to the max number */
    static lgarb_t *iter = &s_oGarbage;
    lgarb_t *grb = (lgarb_t *) malloc(sizeof (lgarb_t));

    grb->garbage = ptrgrb;
    grb->prev = iter;
    grb->next = NULL;
    iter->next = grb;

    iter = iter->next;
    ++s_nLn;
    return;
}

/**
 * Frees all not NULL pointers from the list.
 */
int MGPI_RemoveGarbage(void) {
    lgarb_t *iter = &s_oGarbage;

    while (iter->next) {
        iter = iter->next;
        if (iter->garbage) {
            free(iter->garbage);
            iter->garbage = NULL;
        }
    }

    while (iter->prev) {
        iter = iter->prev;
        if (iter->next) {
        	free(iter->next);
        	iter->next = NULL;
        }
    }
    return s_nLn;
}
